package com.example.proto3;


import com.google.protobuf.Any;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author 23050
 * @version 1.0
 * @description: TODO
 * @date 2023/12/17 17:12
 */
public class TestWrite {
    public static void main(String[] args) throws IOException {
        Contacts.Builder contactsBuilder = Contacts.newBuilder();
        //读取本地已存在的contact.bin  反序列化出通讯录对象
//        Contacts contacts = Contacts.parseFrom(new FileInputStream("src/main/java/com/example/proto3/contacts.bin"));
//        contactsBuilder = contacts.toBuilder();
//

        try {
            contactsBuilder.mergeFrom(new FileInputStream("src/main/java/com/example/proto3/contacts.bin"));
        }catch (FileNotFoundException e){
            System.out.println("contacts.bin 文件没有找到,为您创建新的文件");
        }



        //向通讯录中新增一个联系人
        contactsBuilder.addContacts(addPeopleInfo());
        //序列化,将结果写入文件中
        FileOutputStream outputStream =
                new FileOutputStream("src/main/java/com/example/proto3/contacts.bin");
        contactsBuilder.build().writeTo(outputStream);
        outputStream.close();
        //反序列化

    }

    private static PeopleInfo addPeopleInfo() {
        PeopleInfo.Builder builder = PeopleInfo.newBuilder();
        Scanner scanner = new Scanner(System.in);
        System.out.println("---------新增联系人--------");
        System.out.println("请输入联系人姓名:");
        String name = scanner.nextLine();
        builder.setName(name);
        System.out.println("请输入联系人年龄:");
        int age = scanner.nextInt();
        scanner.nextLine();
        builder.setAge(age);
        for(int i = 0;;i++){
            System.out.println("请输入联系人电话"+(i+1)+"(只输入回车完成电话新增)");
            String numbers = scanner.nextLine();
            if (numbers.isEmpty()){
                break;
            }

            PeopleInfo.Phone.Builder phoneBuilder = PeopleInfo.Phone.newBuilder();
            phoneBuilder.setNumber(numbers);

            System.out.println("请输入此电话类型（1：移动电话  2：固定电话）");
            int type = scanner.nextInt();
            scanner.nextLine();
            switch (type){
                case 1:
                    phoneBuilder.setType(PeopleInfo.Phone.PhoneType.MP);
                    break;
                case 2:
                    phoneBuilder.setType(PeopleInfo.Phone.PhoneType.TEL);
                    break;
                default:
                    System.out.println("选择错误");
                    break;
            }

            builder.addPhone(phoneBuilder);
        }

        Address.Builder addressBuilder = Address.newBuilder();
        System.out.println("请输入联系人家庭地址:");
        String homeAddress = scanner.nextLine();
        addressBuilder.setHomeAddress(homeAddress);
        System.out.println("请输入联系人单位地址:");
        String unitAddress = scanner.nextLine();
        addressBuilder.setUnitAddress(unitAddress);

        builder.setData(Any.pack(addressBuilder.build()));


        System.out.println("请选择要添加的其他联系方式（1.QQ  2.微信）");
        int otherContact = scanner.nextInt();
        scanner.nextLine();
        if (1==otherContact){
            System.out.println("请输入QQ号");
            String qq = scanner.nextLine();
            builder.setQq(qq);
        }else if(2==otherContact){
            System.out.println("请输入微信号");
            String weChat = scanner.nextLine();
            builder.setWechat(weChat);
        }else{
            System.out.println("无效的选择，设置失败");
        }

        for(int i = 0;;i++){
            System.out.println("请输入备注："+(i+1)+"标题(只输入回车完成输入):");
            String key = scanner.nextLine();
            if (key.isEmpty()){
                break;
            }
            System.out.println("请输入备注内容");
            String value = scanner.nextLine();
            builder.putRemark(key,value);
        }



        System.out.println("添加联系人结束");

        return builder.build();
    }
}
